package com.example.bingobox.ui.todobox;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.bingobox.R;
import com.example.bingobox.model.Label;
import java.util.List;
import java.util.Locale;

public class TimeSpanAdapter extends RecyclerView.Adapter<TimeSpanAdapter.ViewHolder> {
    private final List<Label.TimeSpan> timeSpans;
    private final OnTimeSpanActionListener listener;

    public interface OnTimeSpanActionListener {
        void onLabelClick(Label.TimeSpan timeSpan);
        void onDelete(Label.TimeSpan timeSpan);
    }

    public TimeSpanAdapter(List<Label.TimeSpan> timeSpans, OnTimeSpanActionListener listener) {
        this.timeSpans = timeSpans;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_time_label, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Label.TimeSpan timeSpan = timeSpans.get(position);
        
        String timeText = String.format(Locale.getDefault(), "%02d:%02d - %02d:%02d",
            timeSpan.getStartHour(),
            timeSpan.getStartMinute(),
            timeSpan.getEndHour(),
            timeSpan.getEndMinute());
            
        holder.timeLabelText.setText(timeText);
        
        holder.editButton.setOnClickListener(v -> {
            if (listener != null) {
                listener.onLabelClick(timeSpan);
            }
        });
        
        holder.deleteButton.setOnClickListener(v -> {
            if (listener != null) {
                listener.onDelete(timeSpan);
            }
        });
    }

    @Override
    public int getItemCount() {
        return timeSpans.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView timeLabelText;
        ImageButton editButton;
        ImageButton deleteButton;

        ViewHolder(View itemView) {
            super(itemView);
            timeLabelText = itemView.findViewById(R.id.text_time_label);
            editButton = itemView.findViewById(R.id.btn_edit);
            deleteButton = itemView.findViewById(R.id.btn_delete);
        }
    }
} 